package com.lhd.ivos.geofence.service.impl;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.lhd.ivos.geofence.mapper.GeofenceMapper;
import com.lhd.ivos.geofence.pojo.dto.GeofenceParam;
import com.lhd.ivos.geofence.pojo.dto.GeofenceQuery;
import com.lhd.ivos.geofence.pojo.entity.Geofence;
import com.lhd.ivos.geofence.pojo.vo.GeofenceVO;
import com.lhd.ivos.geofence.service.GeofenceService;
import com.lhd.ivos.vehicle.mapper.VehicleMapper;
import com.lhd.ivos.vehicle.pojo.dto.VehicleQuery;
import com.lhd.ivos.vehicle.pojo.vo.VehicleVO;
import com.sun.org.apache.bcel.internal.generic.IF_ACMPEQ;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;

/**
 * @author lhd
 * date 2024/5/23
 * @apiNote
 */
@Slf4j
@Service
public class GeofenceServiceimpl implements GeofenceService {
    @Autowired
    GeofenceMapper geofenceMapper;

    @Autowired
    VehicleMapper vehicleMapper;

    @Override
    public List<GeofenceVO> selectGeofence(GeofenceQuery geofenceQuery) {
        //将查到的多条围栏数据VO对象装到list中,返回给前端
        List<GeofenceVO> geofenceVOList = geofenceMapper.selectGeofence(geofenceQuery);
        // 之前在围栏管理页只查询了围栏名称/坐标/创建时间/状态 并没有对车辆信息进行处理
        // 现在需要给围栏补充车辆总数与可用车辆数的数据
        for(int i = 0; i < geofenceVOList.size();i++){
            // 依次出去每个遍历到的围栏对象
            GeofenceVO geofenceVO = geofenceVOList.get(i);
            // 查询当前围栏上绑定的所有车辆
            VehicleQuery vehicleQuery = new VehicleQuery();
            // 设置车辆查询条件为 当前遍历到的围栏id
            vehicleQuery.setGeofenceId(geofenceVO.getId());
            List<VehicleVO> vehicleVOList = vehicleMapper.selectVehicle(vehicleQuery);
            //获取当前围栏的绑定车辆数
            int totalNum = vehicleVOList.size();
            // 再定义变量保存可用车辆数
            int availableNum = 0;
            for (VehicleVO vehicleVO : vehicleVOList) {
                // 判断车辆状态 1空闲 0占用 3维修
                if ("1.".equals(vehicleVO.getStatus())){
                    availableNum++;
                }
            }
            //给当前围栏对象设置可用车辆数 如果可用车辆数大于0 则说明有可用车辆 否则车用车辆数为0
            if (availableNum>0){
                geofenceVO.setAvailableNum(availableNum);
            }else{
                geofenceVO.setAvailableNum(0);
            }
            //给围栏对象设置车辆总数
            geofenceVO.setTotalNum(totalNum);
            //后续进一步查看车辆详情时会使用车辆数据 所以一起存入geofenceVO返回给前端
            geofenceVO.setVehicleList(vehicleVOList);
        }
        return geofenceVOList;
    }

    @Override
    public void saveGeofence(GeofenceParam geofenceParam) {
        log.debug("围栏信息为:{}",geofenceParam);
        Geofence geofence = new Geofence();
        BeanUtils.copyProperties(geofenceParam,geofence);
        // 前端数据是K-V结构
        //{"geofenceType":"rectangle","recPoints":"116.,116,116,116"}
        HashMap<String,String> position = geofenceParam.getPosition();
        //准备JSON处理库工具
        ObjectMapper objectMapper = new ObjectMapper();
        try {
            // 这个方法如果传入null或者字段不匹配会抛出异常 所以放置于try-catch中来处理即可
            String positionStr = objectMapper.writeValueAsString(position);
            if (geofenceParam.getId()!=null){
                geofenceMapper.update(geofence);
            }else{
                //启用状态
                geofence.setStatus("1");
                geofence.setPosition(positionStr);
                geofence.setCreateTime(new Date());
                geofenceMapper.insert(geofence);
            }
        } catch (JsonProcessingException e) {
            throw new RuntimeException(e);
        }

    }

    @Override
    public void deleteGeofenceById(Long id) {
        log.debug("删除围栏的id为:{}",id);
        VehicleQuery vehicleQuery = new VehicleQuery();
        vehicleQuery.setGeofenceId(id);
        List<VehicleVO> vehicleVOS = vehicleMapper.selectVehicle(vehicleQuery);
        // 删除围栏时，需要将关联的车辆围栏信息删除
        if (vehicleVOS.size()>0 && vehicleVOS!=null){
            throw new RuntimeException("该围栏已被车辆绑定，请先解除绑定");
        }
        geofenceMapper.deleteById(id);
    }
}
